package com.lanou.bbs.controller;

import com.lanou.bbs.entity.BbsReply;
import com.lanou.bbs.entity.BbsTopic;
import com.lanou.bbs.entity.User;
import com.lanou.bbs.service.ReplyService;
import com.lanou.bbs.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 张贵宏
 * @Date 18/6/21 下午2:41
 */
@Controller
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;
    @Autowired
    private TopicService topicService;


    @ResponseBody
    @RequestMapping("/sendReply")
    public void sendReply(BbsReply bbsReply, HttpSession session) {


        BbsTopic bbsTopic = topicService.findById(bbsReply.getTid());
        bbsTopic.setReplyCount(bbsTopic.getReplyCount() + 1);
//        bbsTopic.setReplyCount(+1);
        topicService.updateTopic(bbsTopic);


        User user = (User) session.getAttribute("user");

        Date dt = new Date();
        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
        String format = dFormat.format(dt);
        bbsReply.setrTime(format).setUid(user.getUid());

        replyService.sendReply(bbsReply);


    }


}
